#if 1 //################################### Headers ###############################################################//
#pragma semicolon 1
#include <sourcemod>
#include <sdktools>
#include <smlib>
#include <ctfmod/ctfmod_core>

#undef REQUIRE_PLUGIN 
#include <autoupdate>

#define PLUGIN_VERSION "0.0.1"
#define DEBUG 1

public Plugin:myinfo = {
	name = "CTFmod Classic",
	author = "KawMAN",
	description = "Capture The Flag Classic Mode",
	version = PLUGIN_VERSION,
	url = "http://www.wsciekle.pl/"
};
#endif // ###################### END HEADER ##############################################################################//

#if 1 //################################### Globalss ##############################################################//
new bool:CTFOn							= false;
new bool:g_Enable						= false;

new Handle:g_Cvar_Enable				= INVALID_HANDLE;
new Handle:ReturnTimer[DMAXOBJECTS]		= INVALID_HANDLE;

#endif

#if 1 //*********************************** SourceMOD Fwd *********************************************************//
public OnPluginStart()
{
	if(!LibraryExists("ctfmod_core")) SetFailState("Can't find CTFmod Core Plugin");
	
	g_Cvar_Enable		= CreateConVar("ctf_classic_enable", "1", "");
	
	//Lets Hook CTF Core
	CTFHookObjectCapture(CTFHookObjectCaptureCB);
	CTFHookObjectTake(OnObjectTake);
	CTFHookObjectDrop(OnObjectDrop);
	
	//Remove Buy Zones
	RemoveBuyZone();
	
	//Events
	HookEvent("player_spawn", Event_PlayerSpawn);
	
	//Change 
	new Handle:RoundTime = FindConVar("mp_roundtime");
	SetConVarBounds(RoundTime, ConVarBound_Upper , true, 30.0);
	
	CTFOn = CTF_GetCoreState();
	RefreshSetting();
}

public OnMapStart()
{
	RemoveBuyZone();
	new Handle:RoundTime = FindConVar("mp_roundtime");
	SetConVarBounds(RoundTime, ConVarBound_Upper , true, 30.0);
	if(g_Enable)
	{
		ServerCommand("exec ctfmod/classic_onmapstart.cfg");
		ExecConfig();
		
	}
}

#endif
#if 1 //*********************************** CTF Fwd *********************************************************//
public OnCTFTurnOn()
{
	CTFOn = true;
	RefreshSetting();
}

public OnCTFTurnOff()
{
	CTFOn = false;
	RefreshSetting();
}

public Action:OnObjectTake(&client, &TakenObj)
{
	PrintToChatAll("Object %d Taken by %d",TakenObj, client);
	return Plugin_Continue;
}

public Action:OnObjectDrop(&DropedObj, &client)
{
	//CreateTimer
	return Plugin_Continue;
}
public Action:CTFHookObjectCaptureCB(&client, &CapturedObj, &CapturingObj)
{
	PrintToChatAll("%d %d %d", client, CapturedObj, CapturingObj);
	return Plugin_Continue;
}

#endif

#if 1 //################################### Settings ###############################################################//
RefreshSetting(Handle:convar=INVALID_HANDLE)
{
	if(convar == INVALID_HANDLE || convar == g_Cvar_Enable) 
	{
		decl bool:boolval;
		boolval = GetConVarBool(g_Cvar_Enable);
		if(boolval!=g_Enable)
		{
			if(boolval)
			{
				TurnOn();
			} 
			else
			{
				TurnOff();
			}
		}
		if(convar != INVALID_HANDLE) return;
	}
}
#endif

#if 1 //################################### Standard Events ###############################################################//


#endif

#if 1 //################################### Helpers ###############################################################//
TurnOn()
{
	if(!CTFOn) return;
	g_Enable = true;
	ServerCommand("exec ctfmod/classic_on.cfg");
}

TurnOff()
{
	g_Enable = false;
	ServerCommand("exec ctfmod/classic_off.cfg");
}

RemoveBuyZone()
{
	new entCount = GetMaxEntities();
	decl String:entName[64];
	
	for(new i=0; i<entCount; i++) {
		if(!IsValidEntity(i))
			continue;
		
		GetEdictClassname(i, entName, 63);
		
		if(!StrEqual(entName, "func_buyzone"))
			continue;
		
		RemoveEdict(i); 
	}
}

ExecConfig()
{
	decl String:tmp[MAXCVARLEN], String:MapName[64];
	
}

#endif


